'''
猫眼数据抓取实战
（猫眼电影网站属于美团）
需求分析
百度搜索-猫眼电影-榜单-top100榜单
PS：确定数据在不在页面

https://maoyan.com/board/4?offset=0
https://maoyan.com/board/4?offset=10
https://maoyan.com/board/4?offset=20

offset = (n-1)*10
'''

import requests
import re
import time
import random

class MaoyanSpider:
    def __init__(self):
        '''初始化方法'''
        self.url = "https://maoyan.com/board/4?offset={}"
        self.headers ={
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
        }

    def get_html(self,url):
        html = requests.get(url=url,headers = self.headers).content.decode('utf-8')
        print(html)
        #直接调用解析页面的函数
        self.parse_html(html)

    def parse_html(self,html):
        regex = """<div class="movie-item-info">.*?title="(.*?)".*?<p class="star">主演：(.*?)</p>.*?<p class="releasetime">上映时间：(.*?)</p>"""
       # [（(电影名)，（主演），（上映时间）），（电影2），（电影3）]
        r_list = re.findall(regex,html,re.S)
        self.save_html(r_list)#直接调用保存数据的函数

    def save_html(self,r_list):
        for r in r_list:
            #r:(电影1的爬取内容)
            item = {} #字典
            # strip():取出字符串首尾的空格
            item['title']=r[0].strip()
            item['star']=r[1].strip()
            item['time']=r[2].strip()
            print(item)

    def run(self):
        for page in range(1,3):
            offset = (page-1)*10
            page_url = self.url.format(offset)
            self.get_html(url=page_url)
            time.sleep(random.randint(1,3))

if __name__ == '__main__':
    maoyan = MaoyanSpider()
    maoyan.run()

